package com.itheima.常用API.基本类型包装类;

import java.util.Arrays;
import java.util.Scanner;

public class 二分查找 {
    public static void main(String[] args) {
        //二分查找:大小排列
        int[] a = {21, 5, 4, 11, 33, 22, 32, 23, 44, 66, 6};
        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
        while (true) {
            System.out.println("请输入查找的值:    ");
            Scanner sc = new Scanner(System.in);
            int s = sc.nextInt();
            binarySort(a, s);
        }
//        while (true) {
//            int min = 0, max = a.length - 1;
//            System.out.println("请输入查找数: ");
//            Scanner sc = new Scanner(System.in);
//            int s = sc.nextInt();
//            for (int i = min; i < max; i++) {
//                if (s > a[(max + min) / 2]) {
//                    min = (max + min) / 2 + 1;
//                } else if (s < a[(max + min) / 2]) {
//                    max = (max + min) / 2 - 1;
//                } else {
//                    System.out.println("索引" + i + "的值是:    " + s);
//                    break;
//                }
//            }
//            if (max < min) {
//                System.out.println("没找到........");
//            }
//        }
    }

    public static int binarySort(int[] a, int s) {
        int min = 0, max = a.length - 1;
        for (; min <= max; ) {
            if (s > a[(max + min) / 2]) {
                min = (max + min) / 2 + 1;
            } else if (s < a[(max + min) / 2]) {
                max = (max + min) / 2 - 1;
            } else {
                System.out.println("索引" + min + "的值是:    " + s);
                return min;
            }
            System.out.println(min + "    :   " + max);
            if (max < min) {
                System.out.println("没找到........");
                break;
            }
        }

        return -1;
    }
}
